const express = require("express");
const router = express.Router();
const Response = require("../utils/Response.js");
const fs  = require('fs')

// 引入mysql连接池
const pool = require("../utils/db.js");



// 用户注册
router.post('/register',(req,res,next)=>{
  let {phone,upwd}=req.body
  pool.query('insert into user(phone,upwd) values(?,MD5(?))',[phone,upwd],(err,result)=>{
    if(err){
      return next(err)
    }
    console.log(result);
	if(result.affectedRows===0){
        res.send({code:201,msg:'新增失败'})
      }else{
        res.send({ code: 200, msg: '新增成功',data:result})
      }
  })
})
// 通过手机号获取用户所有信息
router.get('/all',(req,res,next)=>{
	let obj=req.query;
  console.log(obj);
	pool.query('select * from user where phone=?',[obj.phone],(err,result)=>{
		if(err){
			return next(err);
		}
		console.log(result);
		//结果是对象，如果对象下的属性length为0说明修改失败
		if(result.length===0){
			res.send({code:201,msg:'查询失败'});
		}else{
			res.send({code:200,msg:'查询成功',data:result});
		}
	});
})

// 用户登录
router.post('/login',(req,res,next)=>{
  let {phone,upwd}=req.body
  pool.query('select * from user where phone=? and upwd=MD5(?)',[phone,upwd],(err,result)=>{
    if(err){
      return next(err)
    }
    console.log(result);
    if(result.length===0){
      res.send({code:201,msg:'登录失败'})
    }else{
		res.send({ code: 200, msg: '登录成功' ,data:result} )
    }
  })
})

// 个人信息更新
router.post('/information',(req,res,next)=>{
  let obj=req.body
  pool.query('update user set ? where uid=?',[obj,obj.uid],(err,result)=>{
    if(err){
      return next(err)
    }
    console.log(result);
	if(result.affectedRows===0){
        res.send({code:201,msg:'更新失败'})
      }else{
        res.send({ code: 200, msg: '更新成功' })
      }
  })
})

// 通过uid查找用户信息
router.get('/count',(req,res,next)=>{
	let obj=req.query;
  console.log(obj);
	pool.query('select * from user where uid=?',[obj.uid],(err,result)=>{
		if(err){
			return next(err);
		}
		console.log(result);
		//结果是对象，如果对象下的属性length为0说明修改失败
		if(result.length===0){
			res.send({code:201,msg:'查询失败'});
		}else{
			res.send({code:200,msg:'查询成功',data:result});
		}
	});
})


// 用户密码修改
router.post('/setupwd',(req,res,next)=>{
	//2.1获取传递的数据
	let obj=req.body;
	console.log(obj);
	//2.2执行sql命令
	pool.query("update user set upwd=MD5(?) where phone=?",[obj.upwd,obj.phone],(err,result)=>{
		if(err){
			return next(err);
		}
		console.log(result);
		//结果是对象，如果对象下的属性affectedRows为0说明修改失败
		if(result.affectedRows===0){
			res.send({code:201,msg:'修改失败'});
		}else{
			res.send({code:200,msg:'修改成功',data:result});
		}
	});
})

// 查询用户的课程
router.get('/course',(req,res,next)=>{
	let obj=req.query;
  console.log(obj);
	pool.query('select course_project.* from user_course,course_project where uid = ? and pid=course_id;',[obj.uid],(err,result)=>{
		if(err){
			return next(err);
		}
		console.log(result);
		//结果是对象，如果对象下的属性length为0说明修改失败
		if(result.length===0){
			res.send({code:201,msg:'查询失败'});
		}else{
			res.send({code:200,msg:'查询成功',data:result});
		}
	});
})

// 添加用户课程
router.post('/course/add',(req,res,next)=>{
	let obj=req.body
	pool.query('insert into user_course set ?',[obj],(err,result)=>{
	  if(err){
		return next(err)
	  }
	  console.log(result);
	  if(result.affectedRows===0){
		  res.send({code:201,msg:'新增失败'})
		}else{
			
		  res.send({ code: 200, msg: '新增成功',data:result})
		}
	})
  })



module.exports = router;
